﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SeleniumCheckBox.cs" company="">
// </copyright>
// <summary>
//   The selenium check box.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace UITest.Selenium.SeleniumControls.HtmlControls
{
    using OpenQA.Selenium;

    using UITest.Selenium.SeleniumContext;

    /// <summary>
    ///     The selenium check box.
    /// </summary>
    public class SeleniumCheckBox : SeleniumWebObject
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SeleniumCheckBox"/> class.
        /// </summary>
        /// <param name="seleniumContext">
        /// The selenium context.
        /// </param>
        /// <param name="findBy">
        /// The find by.
        /// </param>
        public SeleniumCheckBox(SeleniumContext seleniumContext, By findBy)
            : base(seleniumContext, findBy)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SeleniumCheckBox"/> class.
        /// </summary>
        /// <param name="seleniumContext">
        /// The selenium context.
        /// </param>
        /// <param name="webElement">
        /// The web element.
        /// </param>
        public SeleniumCheckBox(SeleniumContext seleniumContext, IWebElement webElement)
            : base(seleniumContext, webElement)
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets a value indicating whether selected.
        /// </summary>
        public bool Selected
        {
            get
            {
                this.WaitForControlReady();
                return this.GetWebElement().Selected;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     The clear.
        /// </summary>
        public void Clear()
        {
            if (this.Checked)
            {
                this.Click();
                this.JavaScriptExecutor.ExecuteScript("arguments[0].checked=false;", this.FindElement());
            }

            this.Report.WriteTestOutput("Clear on checkbox");
        }

        /// <summary>
        ///     The set.
        /// </summary>
        public void Set()
        {
            if (!this.Checked)
            {
                this.Click();
                this.JavaScriptExecutor.ExecuteScript("arguments[0].checked=true;", this.FindElement());
            }

            this.Report.WriteTestOutput("Set on checkbox");
        }

        #endregion
    }
}